package ihm.views.main;

import ihm.actions.itemTree.SelectedItemAction;
import ihm.controllers.ATKcontroller;

import java.util.List;

import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

import systems.languages.MultiLanguages;
import systems.rpgBuilder.Item;
import systems.rpgBuilder.RpgBuilder;

/**
 * ItemsTree is part of the MainFrame. Then, the itemsTree does not extends 
 * ATKview but require a controller to notify some changement.
 * This part of the view require only to get update when the language used
 * changed. In ordeer to update it, program must call the <i>getPane</i>
 * method.
 * At last, this view is based on Swing Technologies and more specificaly, the
 * JTree and JScrollPane components.
 * @author Baptiste Legrand & Benjamin Orman
 * @version 2.0
 */
public class ItemsTree
{
  //The JTree itself.
  private JTree itemsTree ;
  //The JScrollPane instance
  private JScrollPane itemTreeView;
  //The top (root) of the ItemsTree
  private DefaultMutableTreeNode top;
  //The List of the items used for the ItemsTree creation.
  private List<Item> itemsList;
  //A copy of the MainFrame controller
  private ATKcontroller controller;

  /**
   * Constructor creating a new instance of the ItemsTree.This constructor only
   * do a local copie of the controller and get the ItemList from the 
   * RpgBuilder instance. 
   * If you want to create the JTree and get the panel, you must used <i>getPane
   * </i> method.
   * @param controller - The controller which will be used to notify the change.
   */
  public ItemsTree(ATKcontroller controller)
  {
    this.controller = controller;
    this.itemsList = RpgBuilder.getItemsList();
  }
  /**
   * Builds the tree according to the list of items store in the class and 
   * given at the creation of the ItemsTree.The root node(/item) is given by
   * the RpgBuilder class (see : RpgBuilder.getItemRootMultiLangueName() for 
   * more information).
   * This method also calls the recursive method to build the sons of the root
   * item.
   */
  private void buildTree()
  {
    //Create the top tree node according to the several information
    this.top = new DefaultMutableTreeNode(MultiLanguages.getProperties(RpgBuilder.getItemRootMultiLangueName()));
    //Create the sons of the root node according to the itemList.
    this.buildItemTree(this.itemsList, this.top);
    //Once we get the top node and its sons, we can create the JTree.
    itemsTree = new JTree(top);
    //Set the selection model to SINGLE_TREE_SELECTION which means that the
    //user will be able to select ONE node(/item) at a time.
    itemsTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    //Adding the listener which will treat the item selection action.
    itemsTree.addTreeSelectionListener(new SelectedItemAction(this));
  }
  
  /**
   * Builds recursivly the item tree according to the list of item given 
   * in parameter and the parent node. The item containing in the list will be
   * consider as sons of the parent node and recursly for the ItemsList of each
   * item...
   * @param itemsList - The List of items which will be use to create the tree.
   * @param parent - The parent of the node created in this function.
   */
  private void buildItemTree(List<Item> itemsList, DefaultMutableTreeNode parent)
  {  
    //For each item in the item List ...
    for(Item item : itemsList)
    {
      //... we get the subItemList
      List<Item> subItemsList = item.getSubItemsList();
      //... get the Multilanguage name for this node
      String multilangName = MultiLanguages.getProperties(item.getMultiLangueName());
      //... create the node according to this name
      DefaultMutableTreeNode itemNode = new DefaultMutableTreeNode(multilangName);
      //... build recursivly the subItem of the item previously created
      buildItemTree(subItemsList, itemNode);
      //... and add the item created to the parent node.
      parent.add(itemNode);
    }
  }
  
  /**
   * Generate the tree and return the JScrollPane containing the JTree
   * @return JScrollPane - The JScrollPane containing the ItemsTree (JTree);
   */
  public JScrollPane getPane()
  {
    buildTree();
    itemTreeView = new JScrollPane(itemsTree);
    return itemTreeView;
  }
  
  /**
   * Returns the JTree.
   * @return The ItemsTree
   */
  public JTree getTree()
  {
    return this.itemsTree;
  }
  
  /**
   * Returns the ItemList used to generate the JTree
   * @return JTree - The itemList.
   */
  public List<Item> getItemList()
  {
    return this.itemsList;
  }

  /**
   * Returns the controller of this view.
   * @return ATKcontroller - The Controller of the M-V-C design pattern.
   */
  public ATKcontroller getController()
  {
    return this.controller;
  }
}
